package com.cloudlinkscm.base.authcenter.domain;

import com.cloudlinkscm.base.authcenter.assembler.RoleCreateDto2RoleAssembler;
import com.cloudlinkscm.base.authcenter.assembler.RoleUpdateDto2RoleAssembler;
import com.cloudlinkscm.base.authcenter.constant.RoleStatusEnum;
import com.cloudlinkscm.base.authcenter.dao.RoleDao;
import com.cloudlinkscm.base.authcenter.exception.RoleDeleteException;
import com.cloudlinkscm.base.authcenter.model.Role;
import com.cloudlinkscm.base.authcenter.model.TenantResource;
import com.cloudlinkscm.base.authcenter.model.UserAccount;
import com.cloudlinkscm.base.authcenter.model.dto.RoleCreateDto;
import com.cloudlinkscm.base.authcenter.model.dto.RoleUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : tac
 * @date : 28/07/2017
 */
public class AbstractRoleManager {
    private RoleDao dao;
    private AbstractUserAccountManager userAccountManager;
    private AbstractUserRoleManager userRoleManager;
    private AbstractRolePermissionManager rolePermissionManager;

    @Autowired
    private RoleCreateDto2RoleAssembler roleCreateDto2RoleAssembler;
    @Autowired
    private RoleUpdateDto2RoleAssembler roleUpdateDto2RoleAssembler;

    /**
     * 新建角色
     */
    public Role create(RoleCreateDto dto){
        int rowNum = 0;
        Role role = roleCreateDto2RoleAssembler.assemble(dto);
        supplement(role);
        rowNum += dao.insert(role);
        return role;
    }

    public int delete(String id){
        return delete(id, false);
    }
    
    /**
     * 删除角色
     *
     * <ul>
     *     <li>非强制删除时，只能删除未与用户建立关联关系的角色</li>
     *     <li>强制删除时，将删除已经建立的 用户-角色 关联关系</li>
     *     <li>无论何种删除，都会删除 角色-权限 关联关系</li>
     * </ul>
     * 
     * @param force 是否强制删除
     */
    public int delete(String id, boolean force) {
        int rowNum = 0;
        if (force) {
            rowNum += userRoleManager.deleteByRoleId(id);
            rowNum += doDelete(id);
        } else {
            checkDeleteCondition(id);
            rowNum += doDelete(id);
        }
        return rowNum;
    }

    /**
     * 修改角色信息
     */
    public int update(RoleUpdateDto dto){
        Role o = roleUpdateDto2RoleAssembler.assemble(dto);
        return dao.updateByPrimaryKeySelective(o);
    }

    /**
     * 获取角色关联的用户列表
     */
    public List<UserAccount> listRelativeAccount(String id, int pageIndex, int pageSize){
        return userAccountManager.listByRoleId(id, pageIndex, pageSize);
    }

    /**
     * 获取角色所拥有的资源
     */
    public List<TenantResource> listRoleResource(String id){
        //todo::
        rolePermissionManager.listResourceByRoleId(id);
        return new ArrayList<>();
    }

    /**
     * 检查角色是否可删除
     *
     * @throws RoleDeleteException
     */
    private void checkDeleteCondition(String id) {
        if (userRoleManager.getDao().selectCountByRoleId(id) > 0) {
            //todo:: 后面实现国际化异常信息提示
            throw new RoleDeleteException("已关联用户");
        }
    }

    /**
     * 补全角色默认信息
     */
    private Role supplement(Role role) {
        role.setStatus(RoleStatusEnum.STATUS_ACTIVE.code);
        return role;
    }

    private int doDelete(String id) {
        int rowNum = 0;
        rowNum += rolePermissionManager.deleteByRoleId(id);
        rowNum += dao.deleteByPrimaryKey(id);
        return rowNum;
    }
}
